﻿using BLL;
using Config;
using gfLibrary;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBUtility;
using IYAKASAPortal.Services;

namespace IYAKASAPortal.Charge
{
    public partial class ChargeTJ : System.Web.UI.Page
    {
        private UserInfo userInfo { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
            {
                string userName = Session["UserName"].ToString();
                userInfo = IYAKASAPortal.Services.UserAuthority.GetUserAuthority(userName);
            }
            else
            {
                gfJs.RunFunction(this.Page, "parent.window.location = '../logout.aspx';");
            }
            if (Page.IsPostBack == false)
            {
                BindData();
                btnSearch_Click(sender, e);
            }
        }

        /// <summary>
        /// 为搜索框下拉列表绑定值
        /// </summary>
        private void BindData()
        {
            ListItem item;
            CONST_DEFINE_BLL hpzl = new CONST_DEFINE_BLL();
            EMPLOYEE_USER_BLL employee = new EMPLOYEE_USER_BLL();
            var sfy = employee.GetModelList("LICENSE_NAME like '%,11,%'");
            foreach (var s in sfy)
            {
                item = new ListItem();
                item.Text = s.EMPLOYEE_NAME;
                item.Value = s.EMPLOYEE_NAME;
                drpSFY.Items.Add(item);
            }

            var hp = hpzl.GetModelList("CON_CHNAME='HPZL' order by CON_ORDER asc");
            foreach (var h in hp)
            {
                item = new ListItem();
                item.Text = h.CON_CON;
                item.Value = h.CON_CON;
                drpHPZL.Items.Add(item);
            }

            Dictionary<string, string> sfzl = new Dictionary<string, string>();
            sfzl.Add("全部", "0");
            sfzl.Add("收费", "收费");
            sfzl.Add("退费", "退费");
            drpSFZL.Items.Clear();
            foreach (var z in sfzl)
            {
                item = new ListItem();
                item.Text = z.Key;
                item.Value = z.Value;
                drpSFZL.Items.Add(item);
            }


        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = getSearchSql("search");
            strSql += " order by ID desc";

            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 20);

            rptList.DataSource = m_dtTable;
            rptList.DataBind();

            m_dtTable.Dispose();
        }     

        protected void btnSearchAll_Click(object sender, EventArgs e)
        {
            string strSql = "";
            strSql = getSearchSql("all");
            strSql += " order by ID desc";

            DataTable m_dtTable = new DataTable();
            GreatPager1.BindDataTablePage(ref m_dtTable, AppConfig.g_strConnectionStringSqlClient, strSql, 20);

            rptList.DataSource = m_dtTable;
            rptList.DataBind();

            m_dtTable.Dispose();
        }

        protected void btnPrintAJ_Click(object sender, EventArgs e)
        {
            btnPrintAJ.CssClass = "btnPrintDisabled";
        }

        protected void GreatPager1_PageIndexChanging(object sender, gfPage.PagerEventArgs e)
        {
            GreatPager1.PageIndex = e.NewPageIndex;

            DataTable m_dtTable = GreatPager1.BindDataTablePage();
            rptList.DataSource = m_dtTable;
            rptList.DataBind();
        }

        /// <summary>
        /// 获取收费项目的项目名称
        /// </summary>
        /// <param name="xm">收费项目代码字段</param>
        /// <returns>收费项目名称</returns>
        protected string getXMNmame(string xm)
        {
            string name = "";
            int num = 0;
            string[] results = xm.Split(',');
            CONST_DEFINE_WEB_BLL bll = new CONST_DEFINE_WEB_BLL();
            CONST_DEFINE_WEB bmodel = new CONST_DEFINE_WEB();
            for (int i = 0; i < results.Length; i++)
            {
                if (results[i]!="")
                {
                    name += DbHelper.GetValue("select name from CONST_DEFINE_WEB where code='" + results[i] + "'") + ",";
                    num++;
                }
            }
            return name.Substring(0,name.Length-1);
        }

        /// <summary>
        /// 获取搜索结果对应的总收费额
        /// </summary>
        /// <returns></returns>
        protected string getTotal()
        {
            string strSql = "";
            strSql = getSearchSql("total");
            string total = "";

            total = DbHelper.GetValue(strSql);
            return total;
        }
        
        /// <summary>
        /// 获取不同目的的搜索结果
        /// </summary>
        /// <param name="goal">搜索目的</param>
        /// <returns>搜索结果</returns>
        protected string getSearchSql(string goal)
        {
            //goal=>search:有过滤条件搜索;
            //goal=>all:无过滤条件搜索;
            //goal=>total:获取搜索结果总金额;
            string strSFY = drpSFY.SelectedItem.Value;
            string strHPZL = drpHPZL.SelectedItem.Value;
            string strSFZL = drpSFZL.SelectedItem.Value;
            string strStart = txtStart.Text;
            string strEnd = txtEnd.Text;
            string strKeywords = txtKeywords.Text.Trim();
            string strSql = "";
            if (goal == "total")
            {
                strSql = "select SUM(SFZJE) from CHARGE_INFO where 1=1";              
            }
            else
            {
                strSql = "select ID,SFLSH,HPHM,HPZL,SFZL,SFXM,SFZJE,SFY,SFRQ from CHARGE_INFO where 1=1";
            }

            if (goal != "all")
            {
                #region 有关键字搜索
                if (strKeywords.Length > 0)
                {
                    if (strSFY != "0" && strSFZL != "0" && strHPZL != "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                        strSql += " and (HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY == "0" && strSFZL == "0" && strHPZL == "0")
                    {
                        strSql += "  and (SFY like '%" + strKeywords + "%'";
                        strSql += " or SFZL like '%" + strKeywords + "%'";
                        strSql += " or HPZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY != "0" && strSFZL == "0" && strHPZL == "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += "  and (SFZL like '%" + strKeywords + "%'";
                        strSql += " or HPZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY == "0" && strSFZL != "0" && strHPZL == "0")
                    {
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += "  and (SFY like '%" + strKeywords + "%'";
                        strSql += " or HPZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY == "0" && strSFZL == "0" && strHPZL != "0")
                    {
                        strSql += " and HPZL='" + strHPZL + "'";
                        strSql += "  and (SFY like '%" + strKeywords + "%'";
                        strSql += " or SFZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY != "0" && strSFZL != "0" && strHPZL == "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += "  and (HPZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY != "0" && strSFZL == "0" && strHPZL != "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                        strSql += "  and (SFZL like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                    else if (strSFY == "0" && strSFZL != "0" && strHPZL != "0")
                    {
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                        strSql += "  and (SFY like '%" + strKeywords + "%'";
                        strSql += " or HPHM like '%" + strKeywords + "%'";
                        strSql += " or SFLSH like '%" + strKeywords + "%')";
                    }
                }
                #endregion

                #region 无关键字搜索
                else
                {
                    if (strSFY == "0" && strSFZL == "0" && strHPZL == "0")
                    {
                        //Nothing do
                    }
                    else if (strSFY != "0" && strSFZL != "0" && strHPZL != "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                    }
                    else if (strSFY != "0" && strSFZL == "0" && strHPZL == "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                    }
                    else if (strSFY == "0" && strSFZL != "0" && strHPZL == "0")
                    {
                        strSql += " and SFZL='" + strSFZL + "'";
                    }
                    else if (strSFY == "0" && strSFZL == "0" && strHPZL != "0")
                    {
                        strSql += " and HPZL='" + strHPZL + "'";
                    }
                    else if (strSFY != "0" && strSFZL != "0" && strHPZL == "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and SFZL='" + strSFZL + "'";
                    }
                    else if (strSFY != "0" && strSFZL == "0" && strHPZL != "0")
                    {
                        strSql += " and SFY='" + strSFY + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                    }
                    else if (strSFY == "0" && strSFZL != "0" && strHPZL != "0")
                    {
                        strSql += " and SFZL='" + strSFZL + "'";
                        strSql += " and HPZL='" + strHPZL + "'";
                    }
                }
                #endregion 

                #region 时间搜索
                if (strStart.Length > 0 && strEnd.Length > 0)
                {
                    strSql += " and (";
                    strSql += "SFRQ between '" + strStart + "' and '" + strEnd + "')";
                }
                #endregion
            }
            return strSql;
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {

        }

        //protected void txtEnd_TextChanged(object sender, EventArgs e)
        //{
        //    if (txtStart.Text.Trim() != "" && txtEnd.Text.Trim() != "")
        //    {
        //        DateTime start = DateTime.Parse(txtStart.Text);
        //        DateTime end = DateTime.Parse(txtEnd.Text);
        //        int result = start.CompareTo(end);
        //        if (result > 0)
        //        {
        //            gfAjaxJs.SetAlertMsg(UpdatePanel1, "结束时间必须大于开始时间");
        //            return;
        //        }
        //    }
        //}
      
    }
}